



#include "PosFrm.h"


dbPOS *db;






MainFrm::MainFrm(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &pos, const wxSize &size, long style) 
: MyFrame1(parent, id, title, pos, size, style)
{
	m_grid2->SetTable(0);


}

SettingDlg::SettingDlg(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &pos, const wxSize &size, long style)
: MyDialog2(parent, id, title, pos, size, style)
{
	
}

void SettingDlg::onDialogShow(wxInitDialogEvent &event)
{
	
	

}

void SettingDlg::DialogOkClick(wxCommandEvent &event)
{
	

}




void MainFrm::PosSelect(wxTreeEvent &event)
{
	if(event.GetItem()!= m_treeCtrl3->GetRootItem())
	{
		if (db==NULL)
		{
			dbConnect();
		}
		m_grid2->SetTable(db->GetTable(wxString::Format(GetSettingValue(wxT("SQL1")).GetString(), m_treeCtrl3->GetItemText(event.GetItem()))), true, wxGrid::wxGridSelectRows);
		m_grid2->AutoSizeColumns(false);
		m_grid2->Refresh();
	}
}


void MainFrm::BatchRowSelect(wxGridEvent &event)
{
	if (db==NULL)
	{
		dbConnect();
	}
	m_grid3->SetTable(db->GetTable(wxString::Format(GetSettingValue(wxT("SQL2")).GetString(),m_grid2->GetCellValue(event.GetRow(),0))), true, wxGrid::wxGridSelectRows);
	m_grid3->AutoSizeColumns(false);
	m_grid3->Refresh();
}

void MainFrm::LableClicked(wxGridEvent &event)
{
	if (event.GetCol()>=0)
	{
		((dbTable*)((wxGrid*)event.GetEventObject())->GetTable())->Sort(event.GetCol());
		((wxGrid*)event.GetEventObject())->Refresh();
	}
	
}

void MainFrm::Synch_Menu_Click(wxCommandEvent &event)
{
	GetData();
}


void MainFrm::Synch_Tool_Click(wxCommandEvent &event)
{
	GetData();
}

void MainFrm::Help_Menu_Click(wxCommandEvent &event)
{
	((dbTable*)m_grid2->GetTable())->Sort(0);
	m_grid2->Refresh();
}

void MainFrm::Help_Tool_Click(wxCommandEvent &event)
{

}



void MainFrm::Exit_Menu_Click(wxCommandEvent &event)
{
	this->Close(true);
}


void MainFrm::SettingToolClick(wxCommandEvent &event)
{
	SettingDlg* dlg = new SettingDlg(NULL);
	dlg->ShowModal();
}

bool MainFrm::GetData()
{
	
	if (db==NULL)
	{
		dbConnect();
	}
	switch(m_auinotebook2->GetSelection())
	{
	case 0:
		{
			m_treeCtrl3->DeleteAllItems();
			m_treeCtrl3->AddRoot(wxT("Terminals"));
			wxString t = GetSettingValue(wxT("POSF")).GetString();
			for(int i=1; i<899; i++)
			{
				m_treeCtrl3->AppendItem(m_treeCtrl3->GetRootItem(), wxString::Format(t,i));
			}
			break;
		}
	case 1:
		{
			m_grid31->SetTable(db->GetTable(GetSettingValue(wxT("SQL3"))),true,wxGrid::wxGridSelectRows);
			m_grid31->AutoSizeColumns(false);
			m_grid31->Refresh();
			break;
		}
	case 2:
		{
			m_grid4->SetTable(db->GetTable(GetSettingValue(wxT("SQL4"))),true,wxGrid::wxGridSelectRows);
			m_grid4->AutoSizeColumns(false);
			m_grid4->Refresh();
			break;
		}
	}

	return true;
}


bool MainFrm::dbConnect()
{
	db= new dbPOS(GetSettingValue(wxT("DBHOST")),GetSettingValue(wxT("SID")),GetSettingValue(wxT("DBUSER")),GetSettingValue(wxT("DBPASS")));

	return true;
}